
import bpy

import 全局控制.Node.插座.bl_插座基类 as 插座基类

import Core.bl_UI配色 as UI配色
import engine.initLIB.load_dll as engineLib
import ctypes









class C_Socket_物体(插座基类.C_Socket_基类):
	def __init__(self):
		super(C_Socket_物体, self).__init__()

	def draw(self, context, layout, node, text):
		layout.label(text=text+':Object', icon='OBJECT_DATA')
	def draw_color(self, context, node):
		return (0.3, 0.3, 0.3, 1.0)
		
	def f_getData(self):
		if self.is_linked:
			return engineLib.flexCAPI.f_取插座数据(ctypes.c_wchar_p(self.node.id_data.name), ctypes.c_wchar_p(self.node.name), ctypes.c_wchar_p(self.identifier))
		else:
			return None
		
	def f_setData(self, data):
		engineLib.flexCAPI.f_bl_插座传入数据(
											ctypes.c_wchar_p(self.node.id_data.name), 
											ctypes.c_wchar_p(self.node.name), 
											ctypes.c_wchar_p(self.identifier), 
											ctypes.py_object(data),
											ctypes.c_bool(self.is_output)
		)
		return None












class C_Socket_物体数据(插座基类.C_Socket_基类):
	def __init__(self):
		super(C_Socket_物体数据, self).__init__()

	def draw(self, context, layout, node, text):
		layout.label(text=text+'Data', icon='OBJECT_DATA')
		#self.display_shape = 'SQUARE'
	def draw_color(self, context, node):
		return (0.5, 0.5, 0.5, 1.0)
	def f_getData(self):
		return engineLib.flexCAPI.f_取插座数据(ctypes.c_wchar_p(self.node.id_data.name), ctypes.c_wchar_p(self.node.name), ctypes.c_wchar_p(self.identifier))
		
	def f_setData(self, data):
		return engineLib.flexCAPI.f_bl_插座传入数据(
			ctypes.c_wchar_p(self.node.id_data.name), 
			ctypes.c_wchar_p(self.node.name), 
			ctypes.c_wchar_p(self.identifier), 
			ctypes.py_object(data),
			ctypes.c_bool(self.is_output)
		)









class C_Socket_场景(插座基类.C_Socket_基类):
	def __init__(self):
		super(C_Socket_场景, self).__init__()

	def draw(self, context, layout, node, text):
		layout.label(text=text, icon='SCENE_DATA')
	def draw_color(self, context, node):
		return (0.3, 0.3, 0.3, 1.0)
		
		
	def f_getData(self):
		return engineLib.flexCAPI.f_取物体插座节点数据(ctypes.c_wchar_p(self.node.id_data.name), ctypes.c_wchar_p(self.node.name), ctypes.c_wchar_p(self.identifier))
		
	def f_setData(self):
		return None








class C_Socket_集合(插座基类.C_Socket_基类):
	default_value = bpy.props.PointerProperty(name='物体控制节点树', type=bpy.types.Collection)
	def __init__(self):
		super(C_Socket_集合, self).__init__()

	def draw(self, context, layout, node, text):
		layout.label(text=text, icon='OUTLINER_OB_GROUP_INSTANCE')
	def draw_color(self, context, node):
		return (0.5, 0.5, 0.5, 1.0)
		
	def f_getData(self):
		if self.is_linked:
			return None
			names = engineLib.flexCAPI.f_取物体集插座节点数据(ctypes.c_wchar_p(self.node.id_data.name), ctypes.c_wchar_p(self.node.name), ctypes.c_wchar_p(self.identifier))
			if names :
				loc = bpy.data.collections.find(names)
				if loc >= 0 :
					return bpy.data.collections[loc]
			return None
			#for n in names:
			#return engineLib.flexCAPI.f_取物体集插座节点数据(ctypes.c_wchar_p(self.node.id_data.name), ctypes.c_wchar_p(self.node.name), ctypes.c_wchar_p(self.name))
		else:
			return self.default_value
			
	def f_setData(self, data):
		if self.is_linked:
			pass
		else:
			self.default_value = data
		







class C_Socket_网格(插座基类.C_Socket_基类):
	def __init__(self):
		super(C_Socket_网格, self).__init__()

	def draw(self, context, layout, node, text):
		layout.label(text=text+':mesh', icon='OUTLINER_DATA_MESH')
		#self.display_shape = 'SQUARE'
	def draw_color(self, context, node):
		return (0.3, 0.3, 0.3, 1.0)
		
	def f_getData(self):
		return engineLib.flexCAPI.f_取插座数据(ctypes.c_wchar_p(self.node.id_data.name), ctypes.c_wchar_p(self.node.name), ctypes.c_wchar_p(self.identifier))
		
	def f_setData(self):
		return None











def f_update_插座字体修改(self, context):
	filepath = self.m_字体路径
	字体 = bpy.data.fonts.load(filepath, check_existing=True)
	if 字体 :
		self.default_value = 字体

	if context :
		self.id_data.f_节点数据更新_更新树([self.node])
	
class C_Socket_Bl字体(插座基类.C_Socket_基类):
	#default_value = bpy.props.PointerProperty(type=bpy.types.BlendDataFonts, update=f_update_插座字体修改)
	default_value = bpy.props.PointerProperty(type=bpy.types.VectorFont)
	m_字体路径 = bpy.props.StringProperty(subtype='FILE_PATH', update=f_update_插座字体修改)
	def __init__(self):
		super(C_Socket_Bl字体, self).__init__()

	def draw(self, context, layout, node, text):
		if self.is_output == False and self.is_linked == False and self.default_value == None:
			row = layout.row()
			row.prop(self, 'm_字体路径')
			
		else:
			layout.prop( self, 'default_value', text=text)
			#layout.label(text='字体:'+text)
	def draw_color(self, context, node):
		return (0.3, 0.3, 0.3, 1.0)
		
	def f_getData(self):
		data = engineLib.flexCAPI.f_取插座数据(ctypes.c_wchar_p(self.node.id_data.name), ctypes.c_wchar_p(self.node.name), ctypes.c_wchar_p(self.name), ctypes.c_wchar_p(False))
		if len(data) : return data
		return [self.default_value]
	def f_setData(self):
		return None
	def f_update(self):
		f_update_插座字体修改(self, None)












class C_Socket_平面(插座基类.C_Socket_基类):
	def __init__(self):
		super(C_Socket_平面, self).__init__()

	def draw(self, context, layout, node, text):
		layout.label(text=text+'vec4&&uint32', icon='NORMALS_FACE')
	def draw_color(self, context, node):
		return (0.5, 0.5, 0.5, 1.0)
	def f_str(self):
		return list(f_getData())







class C_Socket_物理材质(插座基类.C_Socket_基类):
	def __init__(self):
		super(C_Socket_物理材质, self).__init__()

	def draw(self, context, layout, node, text):
		layout.label(text=text+'[PyMat]', icon='MATERIAL')
	def draw_color(self, context, node):
		return UI配色.g_1D_物理材质插座颜色


class C_Socket_物理体(插座基类.C_Socket_基类):
	def __init__(self):
		super(C_Socket_物理体, self).__init__()
		#self.display_shape = 'SQUARE_DOT'
	def draw(self, context, layout, node, text):
		layout.label(text=text+'physis', icon='RIGID_BODY')
	def draw_color(self, context, node):
		return UI配色.g_1D_物理体插座颜色
	






classes = (
	C_Socket_物体,
	C_Socket_物体数据,
	
	C_Socket_场景,
	C_Socket_网格,
	#C_Socket_Bl网格,
	
	C_Socket_集合,

	C_Socket_Bl字体,


	
	C_Socket_平面,
	C_Socket_物理材质,
	C_Socket_物理体,
)

def register():
	for c in classes:
		bpy.utils.register_class(c)	

def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)
	

